﻿using BaseicModuleExpression.DBExtend;
using BaseicModuleExpression.Model;
using BaseicModuleExpression.Module;
using System.Linq;
using System.Linq.Expressions;
using BaseicModuleExpression.Sqlsugar;
using SqlSugar;

namespace BaseicModuleExpression
{
    internal class Program
    {
        static void Main(string[] args)
        {
            #region 表达式目录树练习
            {
                //new ExpressionAdd().Run();
            }
            #endregion

            #region 表达式目录树解析sql
            {
                //new ExpreesionSql().Run();
                //ExpressionVisitor访问者类，Visit是个入口，解读表达式的入口
                //1 lambda会区分参数和方法体，调度到更专业访问(处理)方法
                //2 根据表达式的类型，将表达式调度到此类中更专用的访问方法之一的表达式
                //默认的专业处理方法就是按照旧的模式产生一个新的
                //也可以自行扩展，把一些解读操作进行变化
                //表达式目录树是个二叉树，visit做的事儿就是一层层的解析下去，一直到最终的叶节点
                //Expression<Func<User, bool>> expression = x => x.Name.Contains("xx") && x.Age > 0;
                //ExpressionVisitorTest visitorTest = new ExpressionVisitorTest();
                //Expression<Func<int, int, int>> expression = (m, n) => m * n + m/n;
                //Func<int, int, int> data = ((Expression<Func<int, int, int>>)visitorTest.Modify(expression)).Compile();
            }
            #endregion

            #region 表达式链接
            {
                //Expression<Func<User, bool>> expression = x => x.Id == 1;
                //Expression<Func<User, bool>> expression1 = x => x.Id == 2;
                //Expression<Func<User, bool>> expression2 = expression.And(expression1);
                //Expression<Func<User, bool>> expression3 = expression.Or(expression1);
                //Expression<Func<User, bool>> expression4 = expression.Not();
            }
            #endregion

            #region Linq to Sql
            {
                {
                    SqlSugarClient sqlSugarClient = Hepler.GetSugarClient();
                    //sqlSugarClient.Queryable<Orderx>().LeftJoin<User>((o, x) => o.Id == x.Id).LeftJoin<Productx>((x, y, z) => y.Id == z.Id).ToList();
                }
                {
                    (new List<People>()).Where(x => x.Id == 1).ToList();
                    Console.WriteLine((new List<People>()).AsQueryable().Select<People>(x => x.Age > 5
                                                          && x.Id < 5//;
                                                          && x.Id == 3
                                                          && x.Name.StartsWith("1")
                                                          && x.Name.EndsWith("1")
                                                          && x.Name.Contains("1")));
                }
                {
                    Expression<Func<User, People>> expression = x => new People() { Id = x.Id, Age = x.Age };
                    ParseExpression parseExpression = new ParseExpression();
                    parseExpression.LinqToSql(expression);
                }
                {
                    ParseExpression parseExpression = new ParseExpression();
                    Expression<Func<People, bool>> lambda = x => x.Age > 5
                                                        && x.Id < 5//;
                                                        && x.Id == 3
                                                        && x.Name.StartsWith("1")
                                                        && x.Name.EndsWith("1")
                                                        && x.Name.Contains("1");
                    List<People> peoples = new List<People>();
                    //var xx = peoples.AsQueryable().GetType();
                    Console.WriteLine(peoples.AsQueryable().GetType());
                    Console.WriteLine(peoples.AsQueryable().Delete(lambda));
                    Console.WriteLine(peoples.AsQueryable().Select(lambda));
                }
                {
                    //List<User> users = new List<User>();
                    //var data = users.AsQueryable();
                    //data.Select
                    ParseExpression parseExpression = new ParseExpression();
                    Expression<Func<People, bool>> lambda = x => x.Age > 5
                                                        && x.Id < 5//;
                                                        && x.Id == 3
                                                        && x.Name.StartsWith("1")
                                                        && x.Name.EndsWith("1")
                                                        && x.Name.Contains("1");
                    Console.WriteLine(parseExpression.LinqToSql(lambda));


                    Console.WriteLine((new List<People>()).AsQueryable().Select<People>(x => x.Age > 5
                                                        && x.Id < 5//;
                                                        && x.Id == 3
                                                        && x.Name.StartsWith("1")
                                                        && x.Name.EndsWith("1")
                                                        && x.Name.Contains("1")));
                }
                {
                    ParseExpression parseExpression = new ParseExpression();
                    Expression<Func<User, bool>> expression = x => x.Id > 5 && x.Name == "5";
                    Console.WriteLine(parseExpression.LinqToSql(expression));
                }
                {
                    ParseExpression parseExpression = new ParseExpression();
                    Expression<Func<People, bool>> lambda = x => x.Age > 5 && x.Name == "A" || x.Id > 5;
                    Console.WriteLine(parseExpression.LinqToSql(lambda));
                }
                {
                    ParseExpression parseExpression = new ParseExpression();
                    Expression<Func<People, bool>> lambda = x => x.Age > 5 || (x.Name == "A" && x.Id > 5);
                    Console.WriteLine(parseExpression.LinqToSql(lambda));
                }
                {
                    ParseExpression parseExpression = new ParseExpression();
                    Expression<Func<People, bool>> lambda = x => (x.Age > 5 || x.Name == "A") && x.Id > 5;
                    Console.WriteLine(parseExpression.LinqToSql(lambda));
                }

            }
            #endregion
        }
    }
}
